//
//  TKIMCircleProgressView.m
//  TKIM
//
//  Created by liupm on 15/11/23.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMProgressView.h"

@interface TKIMProgressView()

@property(nonatomic,strong) CAShapeLayer *shapeLayer;

@end


@implementation TKIMProgressView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _progressTintColor = [UIColor whiteColor];
        _trackTintColor = [UIColor blackColor];
        _lineWidth = 4.0f;
        _type = TKIMProgressViewTypeNormal;
    }
    return self;
}


-(void)drawRect:(CGRect)rect
{
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _shapeLayer.backgroundColor = _trackTintColor.CGColor;
    UIBezierPath *path ;
    if (_type == TKIMProgressViewTypeCircle)
    {
        _shapeLayer.cornerRadius = _shapeLayer.frame.size.height/2.0;
        path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(_shapeLayer.bounds, 7, 7) cornerRadius:_shapeLayer.frame.size.width/2.0-7];
    }
    else
    {
        path = [UIBezierPath bezierPath];
        [path moveToPoint: CGPointMake(0, (self.frame.size.height-_lineWidth)/2.0)];
        [path addLineToPoint: CGPointMake(_shapeLayer.frame.size.width,(self.frame.size.height-_lineWidth)/2.0)];
        [path closePath];
    }
    
    _shapeLayer.path = path.CGPath;
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.strokeColor = _progressTintColor.CGColor;
    _shapeLayer.lineWidth = _lineWidth;
    _shapeLayer.lineCap = kCALineCapRound;
    _shapeLayer.strokeStart = 0;
    _shapeLayer.strokeEnd = 0;
    [self.layer addSublayer:_shapeLayer];
}



-(void)setProgress:(CGFloat)progress
{
    if (progress < 0.1)
    {
        progress = 0.0;
    }
    else if(progress > 1.0)
    {
        progress = 1.0;
    }
    _progress = progress;
    _shapeLayer.strokeEnd = progress;
}

@end
